#include "kitrobot1.h"
#include <qpainter.h>
#include <QTimer.h>
#include <cmath>
#include <QHBoxLayout>
#include <qpushbutton.h>
#include <qkeyeventtransition.h>
#include <QKeyEvent>

KitRobot1::KitRobot1(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags) 
     // t(new QTimer(this)),
	  //angle(0.0)
{	//frameNo = 0;
	counter = 0;
	angle = 0;
	scale_x = 190;
	scale_y = -20;
	setBackgroundRole(QPalette::Base);
	state = DoNothing;
	position = QPoint(100,100);		
	rectangle.p = QPoint(150, 150);
	arm1.p = QPoint(190, 150);
	arm2.p = QPoint(160, 150);
	arm1.Angle = 0;
	arm2.Angle = 0;
	rectangle.Angle = 0;
	degree = 0;
	ui.setupUi(this);
}

KitRobot1::~KitRobot1()
{

}

QSize KitRobot1 :: sizeHint() const
{
	return QSize(1200, 1200);
}

void KitRobot1 :: DoGoToNeutralPosition()
{
	state = GoingtoPosition;
	position = QPoint(100,100);	
}

void KitRobot1 ::DoGoToDestination(QPoint destination)
{
	state = GoingtoPosition;
	position = destination;
	int x = position.x();
	int y = position.y();

	// length of the arc
	double length = (arm1.p.x()-x)*(arm1.p.x()-x) + ((arm1.p.y()-20)-y)*((arm1.p.y()-20)-y);
	//double length = (40-x)*(40-x) + (-20-y)*(-20-y);
	length = sqrt(double(length));
	// approximate amount of angle (calculated using the lenght of an arc in a circle
	degree = (length*(90)) / (2*3.14*20);
}

void KitRobot1::DoPickUpKit()
{
	state = PickUpKit;
	//kit = k;
}

void KitRobot1::DoDropOffKit(QPoint destination)
{
	state = DropOffKit;
	position = destination;
}

void KitRobot1:: paintEvent(QPaintEvent *)
{
	QPainter painter(this);
	QRect rectang(rectangle.p.x(), rectangle.p.y(), 50, 20);
	 if(state == GoingtoPosition)
	{
		// position of the destination (the kit is placed here)
	//	arm1.p = QPoint(arm1.p.x(),y);
		//degree= 30;
		if(position.x() > rectangle.p.x()){
			if(rectangle.Angle <= degree){
				rectangle.Angle++;
			}
			if(rectangle.Angle >=degree)
			{
			//arm1.p.setX(60);
			//arm1.p.setY(20);
			arm1.p = QPoint(160,20);
			}
		}
		else
		{
			if(rectangle.Angle > -degree){
				rectangle.Angle--;
				flag = true;
			}
			else if(rectangle.Angle <= -degree && flag == true)
			{
				scale_x = position.x();
				scale_y = scale_y-position.y();
				flag = false;
			}
		}

		
		//	painter.restore();
		
	}

	else if(state == PickUpKit)
	{
		//kit = k;
		// enlarge kit
		//k
	}

	else if(state ==DropOffKit)
	{
		// make the kit small
	}
		painter.setPen(QPen(QColor(0, 20/2, 127, 255),3));
		painter.translate(rectangle.p.x()+50/2,rectangle.p.y()+20/2);
		painter.rotate(rectangle.Angle);
		painter.translate(-(rectangle.p.x()+50/2),-(rectangle.p.y()+20/2));
		painter.drawRoundRect(rectang, 5, 10);
		painter.fillRect(rectang, Qt::darkGreen);
		//arm1.p.x()+scale_x
		painter.drawLine(arm1.p.x(), arm1.p.y(), arm1.p.x(), arm1.p.y()+scale_y);
		painter.drawLine(arm2.p.x(), arm2.p.y(), arm2.p.x(), arm2.p.y()-20);
		QTimer::singleShot(10, this, SLOT(update()));
}

void KitRobot1::focusInEvent(QFocusEvent *e)
{
	QMainWindow::focusInEvent(e);
}

void KitRobot1::keyPressEvent(QKeyEvent *e)
{
	switch(e->key())
	{
		case Qt::Key_Space:
				KitRobot1::DoPickUpKit();
				break;
		case Qt::Key_S:
				KitRobot1::DoGoToDestination(QPoint(60, 30));
				break;
		case Qt::Key_D:
			KitRobot1::DoDropOffKit(QPoint(60, 10));
	}
}